import { PrismaBaseService } from '@/prisma-base/prisma-base.service'
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { PassportStrategy } from '@nestjs/passport'
import { ExtractJwt, Strategy } from 'passport-jwt'

@Injectable()
export class JwtSerategy extends PassportStrategy(Strategy, 'jwt') {
  constructor(config: ConfigService, private base: PrismaBaseService) {
    super({
      jwtFromRequest: ExtractJwt.fromHeader('authorization'),
      //加密码的secret
      secretOrKey: config.get('TOKEN_SECRET'),
    })
  }

  async validate({ sub: id }) {
    console.log(id)
    return this.base.wxUser.findUnique({
      where: {
        id,
      },
    })
  }
}
